<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ################



if(Antz_IntelliForm::submitted('new_class')){
	$error = false;

	$classname = post('classname');
	$title = post('title');
	$copy = post('copy');

	$handlerImport = new stdClass();

	if($copy!='#none#'){
		// wanting to copy an existing class
		$copy = $DB->escape($copy);
		$handlerImport = $this->getHandler($copy);
		if(!is_object($handlerImport)){
			$error = true;
			JFX::addError('invalid_copy');
			$handlerImport = new stdClass();
		}
	}else{
		$handlerImport = new stdClass();
		$handlerImport->elements = array();
	}

	if(!preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9\-_]{0,255})$/', $classname)){
		$error = true;
		JFX::addError($this->lang('invalid_classname_pattern'));
	}

	if(trim($classname)==''){
		$error = true;
		JFX::addError($this->lang('empty_classname'));
	}

	if(trim($title)==''){
		$error = true;
		JFX::addError($this->lang('empty_title'));
	}

	if($DB->countRows($CONFIG->dbprefix.$this->keyname.'_classnames', "classname = '{$classname}'")>0){
		// already exists
		$error = true;
		JFX::addError($this->lang('classname_already_exists'));
	}

	if(!$error){
		$handler = new JFX_Module_Blockstyle_Handler($classname);
		$handler->updateFromJson($handlerImport);
		$handler->classname = $classname;
		$dbParams = array(
			'classname' => $classname,
			'sorting' => 0
		);
		$DB->insert($CONFIG->dbprefix.$this->keyname.'_classnames', $dbParams);
		$this->saveHandler($handler);
		$this->updateContentLang('title', $classname, $title, '*', '*');
		JFX::addSuccess($this->lang('new_success'));
	}


}


$options = array('#none#'=>$this->lang('none'));
$allClasses = $this->getAllClassnamesAsArray();
foreach($allClasses as $k=>$row){
	$options[$row['classname']] = $this->getContentLang('title', $row['classname']);
}

$formFields = array(
	array(
		'type' => 'text',
		'name' => 'classname',
		'value' => post('classname'),
		'label' => $this->lang('classname')
	),
	array(
		'type' => 'text',
		'name' => 'title',
		'value' => post('title'),
		'label' => $this->lang('title')
	),
	array(
		'type' => 'select',
		'name' => 'copy',
		'options' => $options,
		'label' => $this->lang('copy'),
		'value' => post('copy', '#none#')
	),
	array(
		'type' => 'submit',
		'name' => 'submitBtn',
		'value' => $this->lang('submit'),
		'label' => '&nbsp;'
	)
);

echo JFX::makeRapidForm($formFields, $this->lang('new_legend'), 'new_class');





